<?php
namespace app\home\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\api\controller\Index;

class Index extends Controller
{
    public function index()
    {
        if (!Session::get('user')) {
           $ses = [];
           $ses['vip'] = 3;
        }else{
            $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
        }
        $data = Db::table('mh_guanggao')->where(['gg_status'=>1])->find();
        $yuanchuang = Db::table('mh_zuopin')->where('fid','eq','1')->select();
        $guoman = Db::table('mh_zuopin')->where('cid','eq',1)->limit(5)->select();
        $riman = Db::table('mh_zuopin')->where('cid','eq',2)->limit(5)->select();
        $zonghe = Db::table('mh_zuopin')->order('mark desc')->limit(5)->select();
        $zuixin = Db::table('mh_zuopin')->order('createtime desc')->limit(6)->select();
        $vip = Db::table('mh_zuopin')->where('vip','eq','2')->limit(6)->select();
        $fenlei = Db::table('mh_fenlei')->select();
        $link = Db::table('mh_ad_link')->select();
        $apii = new Index;
        $api = $apii->callapi();
        return view('index',[
            'data'=>$data,
            'ses'=>$ses,
            'fenlei'=>$fenlei,
            'guoman'=>$guoman,
            'zuixin'=>$zuixin,
            'vip'=>$vip,
            'zonghe' => $zonghe,
            'riman'=>$riman,
            'yuanchuang'=>$yuanchuang,
            'link' =>$link,
            'api'=>$api
        ]) ;
    }

    public function gengxin()
    {
        if (!Session::get('user')) {
            $ses = [];
            $ses['vip'] = 3;
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
        $link = Db::table('mh_ad_link')->select();
        $data = Db::table('mh_guanggao')->where(['gg_status'=>1])->find();
        $zuixin = Db::table('mh_zuopin')->order('createtime desc')->select();
        $fenlei = Db::table('mh_fenlei')->select();

        return view('gengxin',[
            'data'=>$data,
            'fenlei'=>$fenlei,
            'ses'=>$ses,
            'link'=>$link,
            'zuixin'=>$zuixin
        ]);
    }

    public function paihang()
    {
        if (!Session::get('user')) {
            $ses = [];
            $ses['vip'] = 3;
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
        $link = Db::table('mh_ad_link')->select();
        $data = Db::table('mh_guanggao')->where(['gg_status'=>1])->find();
        $guoman = Db::table('mh_zuopin')->where('cid','eq',1)->limit(6)->select();
        $riman = Db::table('mh_zuopin')->where('cid','eq',2)->limit(6)->select();
        $zonghe = Db::table('mh_zuopin')->order('mark desc')->limit(6)->select();      
        $fenlei = Db::table('mh_fenlei')->select();
        return view('paihang',[
            'data'=>$data,
            'ses'=>$ses,
            'fenlei'=>$fenlei,
            'guoman'=>$guoman,
            'riman'=>$riman,
            'zonghe'=>$zonghe,
            'link'=>$link
        ]);
    }

    public function fenlei($id)
    {
        
        if (!Session::get('user')) {
            $ses = [];
            $ses['vip'] = 3;
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
        $data = Db::table('mh_guanggao')->where(['gg_status'=>1])->find();
        $shuju = Db::table('mh_zuopin')->where('fid','eq',$id)->select();
        $fenlei = Db::table('mh_fenlei')->select();
        $link = Db::table('mh_ad_link')->select();
        return view('fenlei',[
            'data'=>$data,
            'ses'=>$ses,
            'fenlei'=>$fenlei,
            'shuju'=>$shuju,
            'link'=>$link
        ]);
    }

    public function user()
    {

        if (!Session::get('user')) {
          $this->success('抱歉请先登录','home/Login/index');
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
        $data = Db::table('mh_in_user')->where(['id'=>$ses['id']])->find();

        $book_data = Db::field('zp.id,zp.pic,zp.name,zp.detail')
                ->table(['mh_user_book'=>'ub','mh_zuopin'=>'zp'])
                ->where('ub.book_id = zp.id')
                ->where(['user_id'=>$ses['id']])
                ->select();
        if ($book_data) {
            return view('user/index',[
                'data'=>$data,
                'ses'=>$ses,
                'book_data'=>$book_data
            ]);
        }else{
            return view('user/index111',[
                'data'=>$data,
                'ses'=>$ses,
            ]);
        }
    }

    public function user_vip(){
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
           }else{
               $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
           }
          $data = Db::table('mh_in_user')->where(['id'=>$ses['id']])->find();
          if ($ses['vip'] == 2) {
              return view('user/user_vip',[
                'data'=>$data,
                'ses'=>$ses
             ]);
          }else{
            return view('user/user_vip_1',[
                'data'=>$data,
                'ses'=>$ses
             ]);
          }
    }

    public function vip()
    {
        $fenlei = Db::table('mh_fenlei')->select();
        $data = Db::table('mh_guanggao')->where(['gg_status'=>1])->find();
        $link = Db::table('mh_ad_link')->select();
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
        $res = Db::table('mh_in_user')->where(['id'=>$ses['id']])->find();
        return view('index/vip_xiangqing',[
                'res'=>$res,
                'data'=>$data,
                'ses'=>$ses,
                'link' =>$link,
                'fenlei'=>$fenlei
        ]);
    }
    
    public function xiugai()
    {
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
        $data = Db::table('mh_in_user')->where(['id'=>$ses['id']])->find();
        return view('user/xiugai',[
            'data'=>$data,
            'ses'=>$ses
        ]);
    }
    
    public function shangchuan()
    {
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
         $data = Db::table('mh_in_user')->where(['id'=>$ses['id']])->find();
         $aaa = Db::table('mh_writer_user')->where(['user_id'=>$ses['id']])->find();
         $bbb = Db::table('mh_writer_apply')->where(['user_id'=>$ses['id']])->find();
         
        if ($aaa) {
            return view('user/shangchuan',['data'=>$data,'aaa'=>$aaa,'bbb'=>$bbb,'ses'=>$ses]);     
        }else{
            if ($bbb) {
                return view('user/quanxian-2',['data'=>$data,'aaa'=>$aaa,'bbb'=>$bbb,'ses'=>$ses]);                
            }else{
                return view('user/quanxian-1',['data'=>$data,'aaa'=>$aaa,'bbb'=>$bbb,'ses'=>$ses]);
            }
        }

        
    }


    public function do_shangchuan(){
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
        $data = Db::table('mh_in_user')->where(['id'=>$ses['id']])->find();

        $xxx =$_POST;
        $img =[] ; 
        $xxx['author'] = $ses['name'] ;
        $file = request()->file('pic');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $xxx['pic'] =  $info->getSaveName();
            }else{
                echo $file->getError();
            }
        }

        $files = request()->file('image');
        foreach($files as $file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'comic');
            if($info){
                $lll = $info->getSaveName();
                $lll = str_replace('\\','/',$lll);
                $img[] =  $lll;
            }else{
                echo $file->getError();
            }
        };

        if (!$img) {
            $this->error('请输入图片');
        }
        if (!array_key_exists('pic',$xxx)) {
            $this->error('请输入封面');
        }
        // 先查看名字   如果有就不上传名字
        $res = Db::table('mh_zuopin')->where(['author'=>$ses['name'],'name'=>$xxx['name']])->find();
        if ($res) {
        //    为真 说明 zuopin表里已经存在   不上传名字直接上传目录
            $aaa = Db::table('mh_mulu')->where(['zid'=>$res['id'],'name'=>$xxx['mulu_name']])->find();
            if ($aaa) {
                $this->error('章节名重复，请修改');
            }
            $time = time();
            $rel_mulu = Db::table('mh_mulu')->insert(['zid'=>$res['id'],'name'=>$xxx['mulu_name'],'uptime'=>$time]);
            // 在目录中上传图片
            $mid = Db::table('mh_zuopin')->getLastInsID();
            foreach ($img as $k => $v) {
                $rel_tupian = Db::table('mh_mcontent')->insert(['mid'=>$mid,'detail'=>$v]);
            }
            if($rel_tupian){
                    $this->success('上传成功', 'home/index/shangchuan');
                } else {
                    $this->error('上传失败');
                }
        }else{
        //    为假  说明作品表里不存在   先上传名字
            $time = time();
            $rel = Db::table('mh_zuopin')->insert(['name'=>$xxx['name'],'author'=>$xxx['author'],'detail'=>$xxx['detail'],'status'=>$xxx['status'],'pic'=>$xxx['pic'],'fid'=>1,'createtime'=>$time]);
            // 得到作品id  然后 上传目录
            $zid = Db::table('mh_zuopin')->getLastInsID();
            $aaa = Db::table('mh_mulu')->where(['zid'=>$zid,'name'=>$xxx['mulu_name']])->find();
            if ($aaa) {
                $this->error('章节名重复，请修改');
            }
            $rel_mulu = Db::table('mh_mulu')->insert(['zid'=>$zid,'name'=>$xxx['mulu_name'],'uptime'=>$time]);
            // 在目录中上传图片
            $mid = Db::table('mh_zuopin')->getLastInsID();
            foreach ($img as $k => $v) {
                $rel_tupian = Db::table('mh_mcontent')->insert(['mid'=>$mid,'detail'=>$v]);
            }
            if($rel_tupian){
                    $this->success('上传成功','home/index/shangchuan');
                } else {
                    $this->error('上传失败');
                }
        }
    }

    public function quanxian_1()
    {
       $user_id = session::get('user')['id'];
       $user_name = session::get('user')['name'];
       $res = Db::table('mh_writer_apply')->insert(['user_id'=>$user_id,'user_name'=>$user_name]);
       if ($res) {
           $this->success('申请成功，请耐心等待','home/index/shangchuan');
       }else{
        $this->success('申请失败，请稍后');
       }
       
    }

    public function update()
    {
        $data = $_POST;
          $file = request()->file('image');
          if($file){
              $info = $file->validate(['size'=>222222,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
              if($info){
                  $data['icon'] = $info->getSaveName();
              }else{
                  echo $file->getError();
              }
          }
        if(!$data['tel']){
            $this->error('请输入电话');
        }

        if($data['pass'] != $data['repass']){
            $this->error('两次密码输入不一样');
        }
        if(!$data['pass']){
            unset ($data['pass']);
        }else{
            $data['pass'] = md5($data['pass']);
        }
        unset ($data['repass']);

        $res = Db::table('mh_in_user')->where(["id"=>$data['id']])->update($data);
        if($res){
            $this->success('修改成功', 'home/index/xiugai');
        } else {
            $this->error('修改失败');
        }


    }

    public function xiangqing($id)
    {
         //查询所有作品 按分数查8个
         $datall = db('zuopin')->limit(8)->order('mark desc')->select();
         //查询所有作品 按时间查8个
         $timeall = db('zuopin')->limit(8)->order('createtime desc')->select();
         //查找该作品信息
         $data1 = db('zuopin')->where('id','eq',$id)->find();
         //查找该分类
         $fname = db('fenlei')->where('id','eq',$data1['fid'])->field('name')->find();
         //查找所有目录 倒叙
         $mulu = db('mulu')->where('zid','eq',$data1['id'])->order('id desc')->select();
         //最新目录
         $xin = db('mulu')->limit(1)->where('zid','eq',$data1['id'])->order('id desc')->find();
 //        dump($xin);die;
         //查找该分类下的5个作品
         $zuo = db('zuopin')->where('fid','eq',$data1['fid'])->limit(5)->order('id desc')->select();
         //查找目录总数
         $cid = db('mulu')->where('zid','eq',$data1['id'])->count('id');
 
         //查看评论
         $comment = db('comment')->where('mid','eq',$id)->paginate(10);
         $zong = db('comment')->where('mid','eq',$id)->count('id');
        $comic = Db::table('mh_zuopin')->where(['id'=>$id])->find();
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
        }else{
            $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
        }
        $data = Db::table('mh_guanggao')->where(['gg_status'=>1])->find();
        $link = Db::table('mh_ad_link')->select();
        $fenlei = Db::table('mh_fenlei')->select();
        if ($comic['vip'] == 1) {
            return view('detail/detail',[
                'id'=>$id,
                'datall'=>$datall,
                'timeall' => $timeall,
                'data1'=>$data1,
                'fname'=>$fname['name'],
                'mulu' => $mulu,
                'cid' => $cid,
                'xin' => $xin,
                'zuo' => $zuo,
                'comment' => $comment,
                'email' => $ses['email'],
                'did' => '4', //登陆用户
                'zong' => $zong,
                'ses'=>$ses,
                'fenlei'=>$fenlei,
                'data'=>$data,
                'link'=>$link
            ]);
        }elseif($comic['vip'] == 2){
         if ($ses['vip'] == 2){
            return view('detail/detail',[
                'id'=>$id,
                'datall'=>$datall,
                'timeall' => $timeall,
                'data1'=>$data1,
                'fname'=>$fname['name'],
                'mulu' => $mulu,
                'cid' => $cid,
                'xin' => $xin,
                'zuo' => $zuo,
                'comment' => $comment,
                'email' => $ses['email'],
                'did' => '4', //登陆用户
                'zong' => $zong,
                'ses'=>$ses,
                'fenlei'=>$fenlei,
                'data'=>$data,
                'link'=>$link
            ]);
                }else{
                    return view('vip_xiangqing',[
                        'id'=>$id,
                        'data'=>$data,
                        'ses'=>$ses,
                        'link'=>$link,
                        'fenlei'=>$fenlei
                    ]);
                }
            
        }
        
       
    }

    public function goumai()
    {
        $fenlei = Db::table('mh_fenlei')->select();
        $link = Db::table('mh_ad_link')->select();
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
        $data = Db::table('mh_guanggao')->where(['gg_status'=>$ses['id']])->find();
        return view('goumai',[
           'ses'=>$ses,
           'data'=>$data,
           'link' =>$link,
           'fenlei'=>$fenlei
        ]);
    }

    public function vip_success()
    {
        $link = Db::table('mh_ad_link')->select();
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
         }else{
             $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
         }
        $rel = Db::table('mh_in_user')->where(['id'=>$ses['id']])->update(['vip'=>2]);
        $res = Db::table('mh_in_user')->where(['id'=>$ses['id']])->find();
        Session::set('user',$res);
        $ses = Session::get('user');
        $link = Db::table('mh_ad_link')->select();
        $fenlei = Db::table('mh_fenlei')->select();
        $data = Db::table('mh_guanggao')->where(['gg_status'=>$ses['id']])->find();
        return view('vip_success',[
           'ses'=>$ses,
           'data'=>$data,
           'link' =>$link,
           'fenlei'=>$fenlei
        ]);
    }

    public function sousuo(){
        $q = $_POST['sousuo'];
        $link = Db::table('mh_ad_link')->select();
        $fenlei = Db::table('mh_fenlei')->select();
        $sousuo = Db::table('mh_zuopin')->where('name','like','%'.$q.'%')->select();
        if ($sousuo) {
            if (!Session::get('user')) {
                $ses = [];
                $ses['vip'] = 3;
             }else{
                 $ses = Db::table('mh_in_user')->where(['id'=>Session::get('user')['id']])->find();
             }
             $data = Db::table('mh_guanggao')->where(['gg_status'=>1])->find();
            return view('index/sousuo',[
                'sousuo'=>$sousuo,
                'data'=>$data,
                'ses'=>$ses,
                'fenlei'=>$fenlei,
                'link'=>$link
            ]);
        }else{
            $this->success('暂未搜索到内容','/home/index/index');
        }
    }




}
